package com.example.week10_spring_security.controller;


import com.example.week10_spring_security.entity.Supplier;
import com.example.week10_spring_security.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RequestMapping("suppliers")
public class SupplierController {

    @Autowired
    SupplierService supplierService;

    @PostMapping("")
    @PreAuthorize("hasAuthority('supplier:add')")
    public ResponseEntity<Void> addSupplier(@RequestBody Supplier supplier) {
        supplierService.addSupplier(supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('supplier:delete')")
    public ResponseEntity<Void> deleteSupplier(@PathVariable long id) {
        supplierService.deleteSupplierById(id);
        return ResponseEntity.ok().build();
    }

    @PutMapping("")
    @PreAuthorize("hasAuthority('supplier:update')")
    public ResponseEntity<Void> updateSupplier(@RequestBody Supplier supplier) {
        supplierService.updateSupplier(supplier);
        return ResponseEntity.ok().build();
    }

    @GetMapping(value="/{id}",produces="application/json;charset=UTF-8")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id) {
        Supplier supplier = supplierService.getSupplierById(id);
        return ResponseEntity.ok(supplier);
    }

}
